package droid.frame.utils.android;


import android.content.Context;
import android.os.Handler;
import android.os.Vibrator;
import droid.frame.App;

/**
 * [项目中用到的震动 单例]<BR>
 * 
 * @author coffee
 */
public class VibratorMgr {

	private static Vibrator mVibrator;

	private VibratorMgr() {
	}

	private static Vibrator getVibrator() {
		if (mVibrator == null) {
			mVibrator = (Vibrator) App.getContext().getSystemService(Context.VIBRATOR_SERVICE);
		}
		return mVibrator;
	}

	/**
	 * 震动(一次)
	 * 
	 * @param milliseconds
	 *            : 震动时长 (毫秒)
	 */
	public synchronized static void vibrate(int milliseconds) {
		getVibrator().vibrate(milliseconds);
	}

	/**
	 * 
	 * @param pattern
	 *            { 500, 100, 500, 100 };
	 * @param repeat
	 */
	public static void vibrate(long[] pattern, int repeat) {
		getVibrator().vibrate(pattern, repeat);
	}

	public static void cancelVibratorDelayed(int milliseconds) {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				getVibrator().cancel();
			}
		}, milliseconds);
	}

	/**
	 * 取消震动
	 */
	public static void cancelVibrator() {
		getVibrator().cancel();
	}
}
